import re
import os


def Get_number_in_filename(inp_list):
    # # 文件夹路径
    # folder_path = './Raw_data'
    # file_names = []

    # # 遍历文件夹，收集所有.txt文件的文件名
    # for filename in os.listdir(folder_path):
    #     if filename.endswith('.txt'):
    #         file_names.append(filename)
    # inp_list=['opt2D_1New_B1_1.txt', 'opt2D_1New_B1_2.txt', 'opt2D_1New_B2_1.txt']


    # 正则表达式模式
    # pattern = re.compile("opt2D_(\d+)(\d+)(\d+)_[A-Za-z]_Ref\.txt")
    # pattern1 = re.compile("opt2D_(\d+)(\d+)(\d+)_[A-Za-z]_(\d+)\.txt")

    # # 提取数字
    # string_numbers = []
    # for file_name in inp_list:
    #     print(f"file_name={file_name}")
    #     match = pattern.search(file_name) or pattern1.search(file_name)
    #     if match:
    #         # 根据匹配的模式，选择不同的组合方式
    #         if match.lastindex >= 4:  # 如果匹配的模式有4个分组
    #             # combined_numbers = match.group(1) + '_' + match.group(2) + '_' + match.group(3) + '_' + match.group(4)
    #             combined_numbers = match.group(1) + match.group(2) + match.group(3)
    #         else:  # 如果匹配的模式有3个分组
    #             # combined_numbers = match.group(1) + '_' + match.group(2) + '_' + match.group(3)
    #             combined_numbers = match.group(1) + match.group(2) + match.group(3)
    #         # print(combined_numbers)
    #         string_numbers.append(combined_numbers)

    # return string_numbers

    pattern = re.compile("opt2D_(.*?)_[A-Za-z]_")

    string_numbers = []
    for file_name in inp_list:
        print(f"file_name={file_name}")
        match = pattern.search(file_name)
        if match:
            # 提取匹配的部分
            combined_numbers = match.group(1)  # 获取opt2D_和_R\G\B_之间的部分
            string_numbers.append(combined_numbers)
    # print(string_numbers)
    return string_numbers

